package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.constant.Url;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.NoticeEventBus.NoticeEvent;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.TeensModeToggleController;
import com.qire.manhua.databinding.ActivityTeensModeToggleBinding;
import com.qire.manhua.model.viewModel.impl.TeensModeToggleViewModel;

import androidx.annotation.Nullable;

/**
 * 青少年模式状态切换
 */
@RouteMapping(path = Navigation.Page.TeensModeTogglePage)
public class TeensModeToggleActivity extends BaseActivity<ActivityTeensModeToggleBinding> {

    @Autowired(injector = EventControllerInjector.class)
    TeensModeToggleController controller;

    @Autowired(injector = ViewModelInjector.class)
    TeensModeToggleViewModel viewModel;

    @Override
    public int layoutRid() {
        return R.layout.activity_teens_mode_toggle;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        viewModel.onAddition();
        controller.onAddition(viewModel);

        if(TeensModeManage.teensModeEnable()) {
            viewModel.showDisablePanel();
        } else {
            viewModel.showEnablePanelOne();
        }
    }

    @Override
    public void finish() {
        if(viewModel.canBack()) {
            return;
        }
        super.finish();
    }

    @Override
    protected void registerObserver() {
        super.registerObserver();
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.TEENS_MODE_TOGGLE, this::onToggleCallback);
    }

    @Override
    protected void unregisterObserver() {
        super.unregisterObserver();
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.TEENS_MODE_TOGGLE);
    }

    /**
     * 响应总线通知：来自{@link com.qire.manhua.dal.service.TeensModeService TeensModeService}
     * @param event 携带的数据如果为 true 则表示是开启接口的返回，如果为 false 则表示是禁用接口的返回。
     * @return boolean 事件是否需要继续传递给其他订阅者处理。true 不需要已处理了 false 需要
     */
    private boolean onToggleCallback(NoticeEvent<Boolean> event) {
        boolean enable = event.getEventData();
        String msg = enable ? "青少年模式已开启" : "青少年模式已关闭";
        ToastHelper.showSnackBarAtPreviousPage(msg, Prompt.SUCCESS);
        super.finish();
        return true;
    }

    @DalExceptionObserve(tags = {Url.TeensModeEnable, Url.TeensModeDisable})
    public void teensModeToggleExceptionHandle(DataAccessException e) {
        ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
    }

}
